{% extends '../_manage.html' %}

{% block title %} {{ form.name }} {% endblock %}

{% block head %}
<script>

var id = parseInt('{{ id }}');

$(function () {
    if (id > 0) {
        getJSON('/api/wikis/' + id, function (err, wiki) {
            if (err) {
                return showError(err);
            }
            wiki.publishAtLocal = timestampToDateTimeLocal(wiki.publishAt);
            initVM({
            	title: 'Edit Wiki',
            	wiki: wiki
            });
        });
    }
    else {
        initVM({
        	title: 'New Wiki',
            wiki: {
            	publishAtLocal: timestampToDateTimeLocal(Date.now()),
            	content: ''
            }
        });
    }
});

function initVM(data) {
    if (data.wiki.imageId) {
        $('#image-preview').css('background-image', 'url(/files/attachments/' + data.wiki.imageId + '/l)');
    }
    window.vm = new Vue({
        el: '#vm',
        data: {
        	title: data.title,
            wiki: data.wiki
        },
        created: function () {
            $('#loading').hide();
            $('#vm').show();
        },
        methods: {
            submit: function() {
                var that = this;
                clearFormError();
                this.wiki.publishAt = dateTimeLocalToTimestamp(this.wiki.publishAtLocal);
                this.wiki.content = $('#content-editor').val();
                that.$resource('{{ action }}').save(this.wiki).then(function (resp) {
                    resp.json().then(function (result) {
                        that.saved = true;
                        location.assign(id > 0 ? '/manage/wiki/wiki_tree?id=' + id : '/manage/wiki/');
                    });
                }, onJsonFormError);
            }
        }
    });


    // check content when unload:
    window.onbeforeunload = function() {
        if (window.vm.saved) {
            return;
        }
        if (window.vm.wiki.content !== $('#content-editor').val()) {
            return "{{ _('Content has been changed.') }}";
        }
    };

    $('#content-editor').val(window.vm.wiki.content || '');
    var htmleditor = UIkit.htmleditor($('#content-editor').get(0), {
        markdown: true,
        maxsplitsize: 600
    });
    // override image action:
    htmleditor.off('action.image');
    htmleditor.on('action.image', function() {
        uploadImage(function (err, result) {
            if (err) {
                return;
            }
            htmleditor['replaceSelection']('\n![' + result.name + '](' + result.url + ')\n');
        });
    });
    window.htmleditor = htmleditor;

    // watch file change:
    $('#file').change(function (evt) {
        evt.preventDefault();
        var f = $('#file').val();
        if (f === '') {
            // user cancelled selected file:
            vm.wiki.image = null;
            $('#image-preview').css('background-image', 'none');
            return;
        }
        try {
            var file = $('#file').get(0).files[0];
            processImageFile(file, function (r) {
                vm.wiki.image = r.data;
                $('#image-preview').css('background-image', 'url(' + r.image + ')');
            });
        }
        catch (e) {
            UIkit.modal.alert('Error when process file: ' + e);
        }
    });
}
</script>

{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> {{ _('Loading') }}...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-breadcrumb">
            <li><a href="/manage/wiki/">{{ _('All Wikis') }}</a></li>
            <li v-if="wiki.id"><a v-bind:href="'/manage/wiki/wiki_tree?id='+wiki.id" v-text="wiki.name"></a></li>
            <li class="uk-active"><span v-text="wiki.name"></span></li>
        </ul>

        <form v-on:submit.prevent="submit" class="uk-form uk-form-stacked uk-margin">
            <legend v-text="title"></legend>
            <fieldset>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Image') }}:</label>
                    <div class="uk-form-controls">
                        <div id="image-preview" style="border: 1px solid #ccc; width:642px; height:362px; background-size:cover;"></div>
                    </div>
                    <div class="uk-form-controls">
                        <input id="file" type="file">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Name') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="wiki.name" name="name" type="text" maxlength="100" class="uk-width-1-1" placeholder="{{ _('Name') }}">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="control-label">{{ _('Tag') }}:</label>
                    <div class="controls">
                        <input v-model="wiki.tag" name="tag" type="text" class="uk-width-1-1" placeholder="tag">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="control-label">Publish At:</label>
                    <div class="controls">
                        <input v-model="wiki.publishAtLocal" name="publishAtLocal" type="datetime-local">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Description') }}:</label>
                    <div class="uk-form-controls">
                        <textarea v-model="wiki.description" name="description" rows="6" class="uk-width-1-1" style="resize:none;"></textarea>
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="control-label">{{ _('Content') }}:</label>
                    <div class="controls">
                        <textarea id="content-editor" rows="20" style="width:100%; resize:none;"></textarea>
                    </div>
                </div>
                <div class="uk-form-row">
                    <div class="controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> {{ _('Save') }}</button>
                        <button type="button" class="uk-button" onclick="location.assign(window.id > 0 ? '/manage/wiki/wiki_tree?id=' + window.id : '/manage/wiki/')"><i class="uk-icon-times"></i> {{ _('Cancel') }}</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>

{% endblock %}
